import Vue from 'vue'
import VueRouter from 'vue-router'
import request from "@/utils/request";
import store from "@/store";

Vue.use(VueRouter)

const routes = [
  // 默认路由
  {
    path: '/',
    redirect: "/Home"
  },

  {
    path:'/home',
    name:'Home',
    meta:{title:'MedicalPro'},
    component: () => import('../views/Home.vue')
  },
  {
    path:'/login',
    name:'Login',
    meta:{title:'登录'},
    component: () => import('../views/Login.vue')
  },
  {
    path:'/mainpage',
    name:'MainPage',
    meta:{title:'主页'},
    component: () => import('../views/MainPage.vue'),
    children:[
        {path: 'clock', meta: {title: '主页'}, name: 'Clock', component: ()=>import('../components/Clock')},
        {path:'hospitallist',meta: {title: '医疗机构管理'},name: 'HospitalList',component: ()=>import('../views/HospitalList')},
        {path:'note',meta:{title:'通知'}, name:'Note', component: () => import('../views/Note.vue')},
        {path:'userlist', name:'UserList',meta:{title:'用户管理'}, component: () => import('../views/UserList.vue')},
        {path:'departmentlist', name:'DepartmentList',meta:{title:'部门管理'}, component: () => import('../views/DepartmentList.vue')},
        {path:'templatelist', name:'TemplateList',meta:{title:'报表模板管理'}, component: () => import('../views/TemplateList.vue')},
        {path:'reportlist', name:'ReportList', meta:{title:'用户报表管理'}, component: () => import('../views/ReportList.vue')},
        {path:'reportmanage/:id', name:'ReportManage', meta:{title:'用户报表审核'}, component: () => import('../views/ReportManage.vue')},
        {path:'drnumchart', name:'drNumChart', meta:{title:'医生人数折线图'}, component: () => import('../views/drNumChart.vue')},
        {path:'userreportlist', name:'UserReportList', meta:{title:'报表填写'}, component: () => import('@/views/UserReportList.vue')},
        {path:'userreportmanage', name:'UserReportManage', meta:{title:'报表查看'}, component: () => import('@/views/UserReportManage.vue')},
        {path:'businesslist', name:'BusinessList', meta:{title:'用户事务管理'}, component: () => import('../views/BusinessList.vue')},
        {path:'userbusinesslist', name:'UserBusinessList', meta:{title:'事务查看'}, component: () => import('../views/UserBusinessList.vue')}
    ]
  }
]

const router = new VueRouter({
  routes
})


//白名单
const whiteRouter = ['/login','/Home','/'];

router.beforeEach((to,from,next)=>{
  if(to.path.startsWith('/Login')){
    localStorage.removeItem('access-admin')
    next()
  }
  else if(whiteRouter.indexOf(to.path) !== -1){
    next()
  }
  else {
    //校验token合法性
    let user = JSON.parse(window.localStorage.getItem('access-admin'))
    if(!user){
      next({path: '/Login'})
      window.alert('校验失败请重新登录')

    }
    else{
      store.state.role = user.role
      store.state.id = user.id
      store.state.nickname = user.nickname
      request({
        url: '/user/checktoken',
        method: 'get',
        headers: {
          password: user.password
        }
      }).then((response)=>{
        if(response.data.code === "401"){
          console.log(response)
          console.log('校验失败请重新登录')
          window.alert('校验失败请重新登录')
          next({path: '/Login'})
        }
      })
      next()
    }
  }

})

router.afterEach((to,from)=>{

  document.title = to.meta.title || 'MedicalPro'
})

export default router
